package cc.mcggdd.leah.boot.stock.service.impl;

import cc.mcggdd.leah.boot.core.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.mcggdd.leah.boot.stock.mapper.StockReceivableMapper;
import cc.mcggdd.leah.boot.stock.service.StockReceivableService;
import cc.mcggdd.leah.boot.stock.model.entity.StockReceivable;
import cc.mcggdd.leah.boot.stock.model.form.StockReceivableForm;
import cc.mcggdd.leah.boot.stock.model.query.StockReceivableQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockReceivableVO;
import cc.mcggdd.leah.boot.stock.converter.StockReceivableConverter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 应收服务实现类
 *
 * @author 
 * @since 2025-02-08 17:52
 */
@Service
@RequiredArgsConstructor
public class StockReceivableServiceImpl extends ServiceImpl<StockReceivableMapper, StockReceivable> implements StockReceivableService {

    private final StockReceivableConverter stockReceivableConverter;

    /**
    * 获取应收分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<StockReceivableVO>} 应收分页列表
    */
    @Override
    public IPage<StockReceivableVO> getStockReceivablePage(StockReceivableQuery queryParams) {
        Page<StockReceivableVO> pageVO = this.baseMapper.getStockReceivablePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取应收表单数据
     *
     * @param id 应收ID
     * @return
     */
    @Override
    public StockReceivableForm getStockReceivableFormData(Long id) {
        StockReceivable entity = this.getById(id);
        return stockReceivableConverter.toForm(entity);
    }
    
    /**
     * 新增应收
     *
     * @param formData 应收表单对象
     * @return
     */
    @Override
    public boolean saveStockReceivable(StockReceivableForm formData) {
        StockReceivable entity = stockReceivableConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }
    
    /**
     * 更新应收
     *
     * @param id   应收ID
     * @param formData 应收表单对象
     * @return
     */
    @Override
    public boolean updateStockReceivable(Long id,StockReceivableForm formData) {
        StockReceivable entity = stockReceivableConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }
    
    /**
     * 删除应收
     *
     * @param ids 应收ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteStockReceivables(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的应收数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public BigDecimal getStockReceivableTotalAmount(String status) {
        BigDecimal totalAmount = this.baseMapper.getStockReceivableTotalAmount(status);
        if(totalAmount == null) {
            totalAmount = BigDecimal.ZERO;
        }
        return totalAmount;
    }

}
